package com.zdk.mdm.engine;

import android.annotation.TargetApi;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.UserHandle;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import cn.com.cetccst.mdmdemo.utils.Logger;

public class MdmDeviceAdminReceiver extends DeviceAdminReceiver {
    public static final String ACTION_DEVICE_ADMIN_ENABLED = "cetc.action.device.admin.enable";

    public static final IntentFilter receiverFilter = new IntentFilter(ACTION_DEVICE_ADMIN_ENABLED);

    public static ComponentName getComponentName(Context context) {
        return new ComponentName(context.getApplicationContext(), MdmDeviceAdminReceiver.class);
    }

    @Override
    public void onEnabled(Context context, Intent intent) {
        Logger.log("device admin has been enabled by user.");
        LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(ACTION_DEVICE_ADMIN_ENABLED));
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        Logger.log("Device Admin has been disabled.");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        Logger.log("Device password input failed.");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        Logger.log("Device password has been validated.");
    }

    @Override
    public void onPasswordExpiring(Context context, Intent intent) {
        DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        long expr = dpm.getPasswordExpiration(getComponentName(context));
        long delta = expr - System.currentTimeMillis();
        boolean expired = delta < 0L;
        Logger.log("Device password expired: " + expired);
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        Logger.log("Device password has been changed.");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        super.onDisableRequested(context, intent);
        Logger.log("User is request to disable admin.");
        return "";
    }

    @TargetApi(Build.VERSION_CODES.O)
    @Override
    public void onUserAdded(Context context, Intent intent, UserHandle newUser) {
        Logger.log("new User is added " + newUser);
    }

    @TargetApi(Build.VERSION_CODES.O)
    @Override
    public void onUserRemoved(Context context, Intent intent, UserHandle removedUser) {

    }

    public void onLockTaskModeEntering(Context context, Intent intent, String pkg) {
        Logger.log("User is enter LockTaskMode " + pkg);
    }

    public void onLockTaskModeExiting(Context context, Intent intent) {
        Logger.log("User is exit LockTaskMode");
    }


}
